//
//  AppDelegate.swift
//  Three-SinaWeibo
//
//  Created by 李旭阳 on 15/11/11.
//  Copyright © 2015年 apple. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        let account = UserAccount.loadAccount()
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.backgroundColor = UIColor.whiteColor()
        window?.makeKeyAndVisible()
        print(account)
        //设置主题颜色
        setThemeColor()
        
        //注册通知
        registerNotification()
        
        window?.rootViewController = defaultRootController()
        
        return true
    }
    
    //MARK: 发送通知
    private func registerNotification(){
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "switchToRootVC:", name: AppSwitchRootViewControler, object: nil)
    }
    
    //MARK:  监听通知 n 是一个参数
    @objc private func switchToRootVC(n: NSNotification) {
        //跳转到MainVC
        if (n.object as? String ) != nil{
            window?.rootViewController = WelComeViewController()
            return
        }
        window?.rootViewController = MainViewController()
    }

    //MARK: 移除通知
    //因为 appdelegate 是一个单例对象 所以这里移不移除通知都一样 编码规范而已 没有任何作用
    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    //MARK: 判断用户是否登陆 并且判断是否更新版本
    private func defaultRootController() -> UIViewController {
        //判断用户是否登录了
        if UserAccountViewModel().userLogin {
            //方法一
            //如果登录了 判断是否是新版本
            //if isNewVersion() {
                //return NewFeatureViewController()
            //}
            //return WelComeViewController()
            //方法二
            return isNewVersion() ? NewFeatureViewController() : WelComeViewController()
        }
        return MainViewController()
    }
    
    //MARK: 检查是否是新版本
    private func isNewVersion() -> Bool{
        //比较 新旧版本
        //获取新版本
        let versionStr = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        //将 版本号 转换成 double 类型
        let versionNum = Double(versionStr)
        
        //获取旧版本
        let key = "sandBoxKey"
        let userdefaults = NSUserDefaults.standardUserDefaults()
        let lastVersion = userdefaults.doubleForKey(key)
        
        //保存新版本
        userdefaults.setDouble(versionNum!, forKey: key)
        //同步到沙盒中
        userdefaults.synchronize()
        
        //比较新旧版本
        return versionNum! > lastVersion
    }
    
    //MARK: 主体颜色
    private func setThemeColor(){
        UINavigationBar.appearance().tintColor = themeColor
        UITabBar.appearance().tintColor = themeColor
        
    }

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

